package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.ManualModificationUserLevelBo;
import org.dromara.system.domain.bo.SimUserLevelRecordsBo;
import org.dromara.system.domain.bo.UserLevelBo;
import org.dromara.system.domain.vo.SimUserLevelRecordsVo;
import org.dromara.system.service.ISimUserLevelRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 等级记录
 *
 * @author Lion Li
 * @date 2025-09-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userLevelRecords")
public class SimUserLevelRecordsController extends BaseController {

    private final ISimUserLevelRecordsService simUserLevelRecordsService;

    /**
     * 查询等级记录列表
     */
//    @SaCheckPermission("system:userLevelRecords:list")
    @GetMapping("/list")
    public TableDataInfo<SimUserLevelRecordsVo> list(SimUserLevelRecordsBo bo, PageQuery pageQuery) {
        return simUserLevelRecordsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出等级记录列表
     */
//    @SaCheckPermission("system:userLevelRecords:export")
    @Log(title = "等级记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SimUserLevelRecordsBo bo, HttpServletResponse response) {
        List<SimUserLevelRecordsVo> list = simUserLevelRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "等级记录", SimUserLevelRecordsVo.class, response);
    }

    /**
     * 获取等级记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userLevelRecords:query")
    @GetMapping("/{id}")
    public R<SimUserLevelRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(simUserLevelRecordsService.queryById(id));
    }

    /**
     * 新增等级记录
     */
//    @SaCheckPermission("system:userLevelRecords:add")
    @Log(title = "等级记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SimUserLevelRecordsBo bo) {
        return toAjax(simUserLevelRecordsService.insertByBo(bo));
    }

    /**
     * 修改等级记录
     */
//    @SaCheckPermission("system:userLevelRecords:edit")
    @Log(title = "等级记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SimUserLevelRecordsBo bo) {
        return toAjax(simUserLevelRecordsService.updateByBo(bo));
    }

    /**
     * 删除等级记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userLevelRecords:remove")
    @Log(title = "等级记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(simUserLevelRecordsService.deleteWithValidByIds(List.of(ids), true));
    }

    @Log(title = "修改用户等级", businessType = BusinessType.INSERT)
    @PostMapping("/updateUserLevel")
    public R<Boolean> updateUserLevel(@RequestBody UserLevelBo bo) {
        return R.ok(simUserLevelRecordsService.updateUserLevel(bo));
    }


    @PostMapping("/manualModificationUserLevel")
    public R<Boolean> manualModificationUserLevel(@RequestBody @Validated ManualModificationUserLevelBo bo) {
        return R.ok(simUserLevelRecordsService.manualModificationUserLevel(bo));
    }


}
